package team.bluepen.order.web.servlet.user;

import team.bluepen.order.ErrorPack;
import team.bluepen.order.constant.ErrorCode;
import team.bluepen.order.data.database.mysql.UserRepository;
import team.bluepen.order.data.entity.User;
import team.bluepen.order.util.StringUtil;
import team.bluepen.order.web.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author RollW
 */
@WebServlet(name = "UserDeleteServlet", urlPatterns = "/api/user/delete")
public class UserDeleteServlet extends BaseServlet {
    public static final String ID_PARAM = "id";

    @Override
    protected void post(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter(ID_PARAM);
        setJsonHeader(resp);
        if (StringUtil.isEmpty(id)) {
            writeJson(resp, new ErrorPack(ErrorCode.ERROR_PARAM_MISSING,
                    "Missing id param.").toCodePack());
            return;
        }
        UserRepository repository = new UserRepository();
        repository.getUserDao().delete(Long.parseLong(id));
        writeJson(resp, new ErrorPack(ErrorCode.SUCCESS,
                "SUCCESS").toCodePack());
    }
}
